package com.flyee.controller.tools.cartoon.ui.changeequip.control
{
	import com.flyee.controller.tools.cartoon.ui.changeequip.view.SelectItemContainer;
	
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	public class SeleteItemManager
	{
		private static var instance:SeleteItemManager = null;
		
		private var groupDic:Dictionary = new Dictionary();
		
		public function SeleteItemManager()
		{
			if (instance)
			{
				throw new Error("singleton error!!!");
			}
			instance = this;
		}
		public static function getInstance():SeleteItemManager
		{

			if (instance == null)
			{
				instance = new SeleteItemManager();
			}
			return instance;
		}
		public function addChild(item:SelectItemContainer,group:String):void
		{
			var tempArray:Array = null;
			if(groupDic[group] == undefined || groupDic[group] == null)
			{
				tempArray = new Array();
				groupDic[group] = tempArray;
			}
			else
			{
				tempArray = groupDic[group];
			}
			if(item)
			{
				item.myGroup = group;
				var tempLength:uint = tempArray.length;
				for(var i:int = 0; i < tempLength; i++)
				{
					if(tempArray[i] == item)
					{
						return;
					}
				}
				item.addEventListener(MouseEvent.CLICK,clickHandle);
				tempArray.push(item);
			}
		}
		private function getItemInArray(item:SelectItemContainer,tagArray:Array):Number
		{
			if(tagArray)
			{
				var tempLength:uint = tagArray.length;
				for(var i:int = 0; i < tempLength; i++)
				{
					if(tagArray[i] == item)
					{
						return i;
					}
				}
			}
			return -1;
		}
		private function clickHandle(e:MouseEvent):void
		{
			var tempItem:SelectItemContainer = e.currentTarget as SelectItemContainer;
			var tempArray:Array = this.groupDic[tempItem.myGroup];
			var tempLength:uint = tempArray.length;
			for(var i:int = 0; i < tempLength ; i ++)
			{
				if(tempArray[i] != null)
				{
					if(tempArray[i] != tempItem)
					{
						tempArray[i].isSelect = false;
					}
				}
			}
		}
		public function removeChild(item:SelectItemContainer):void
		{
			if(item != null)
			{
				var tempArray:Array = this.groupDic[item.myGroup];
				var tempLength:uint = tempArray.length;
				for(var i:int = 0; i < tempLength ; i ++)
				{
					if(tempArray[i] == item)
					{
						tempArray.splice(i,1);
						item.removeEventListener(MouseEvent.CLICK,clickHandle);
						return;
					}
				}
			}
		}
		public function clear():void
		{
			for(var i:* in this.groupDic)
			{
				var tempArray:Array = this.groupDic[i];
				var tempLength:uint = tempArray.length;
				for(var j:int = 0; j < tempLength ; j ++)
				{
					tempArray[j].removeEventListener(MouseEvent.CLICK,clickHandle);
				}
				delete this.groupDic[i];
			}
		}
	}
}